<?php
/**
 * 会员注册
 */
namespace Home\Controller;
use Common\Controller\HomebaseController;
use Common\Model\UsersModel;
class RegisterController extends HomebaseController {
	public function index()
	{
		$this->display();
	}
	/**
	 * AJAX 登录操作
	 */
	public function do_login(){

		if (IS_AJAX) {
			$UserModel = new UsersModel();
			$url = __ROOT__."/";
			if ($UserModel->do_login()) {
				exit(json_encode(array('status'=>1,'url'=>$url,'msg'=>'登录成功,马上为您跳转..^_^')));
			} else {
				//验证失败
				exit(json_encode(array('status'=>0,'url'=>'','msg'=>$UserModel->getError())));
			}
		}
	}

	//加载注册第一步模板
	public function do_mywap_register()
	{
		$this->display();
	}
    public function wap_register()
	{
		//获取手机号
        $tel = $_POST['tel'];
		//生成随机数字；
		$code = mt_rand(1000,9999);
		//将用户发送的验证码和手机屏凑成字符串放到session中以便调用
		$_SESSION['phonecode']=$tel.$code;

		if($tel){
			set_time_limit(0);
			header('Content-Type: text/plain; charset=utf-8');
			vendor('Alidayu.ApiDemo.SmsDemo');
			$demo = new \SmsDemo(
				"LTAIfbz43deVziQK",
				"nDTJMzGIcTXXKauhPdPCnVNwDUC2lj"
			);
			$response = $demo->sendSms(
				"校长学院", // 短信签名
				"SMS_123672947", // 短信模板编号
				"$tel", // 短信接收者
				Array(  // 短信模板中字段的值
					"code"=>"$code",
					"product"=>"dsd"
				),
				"123"
			);
			echo "发送短信(sendSms)接口返回的结果:\n";
			//print_r($response);
			if($response)
			{
				exit(json_encode(array('status'=>1,'url'=>'','msg'=>'验证码发送成功,赶紧注册吧..^_^')));
			}
		}

		// return $this->display();

	}
	//对手机号和验证码进行验证正确与否
	public function verifyphonecode()
	{
		if (IS_AJAX) 
		{
			$phone = $_POST['phone'];
			$code = $_POST['code'];
			$compcode = $phone.$code;
			if($_SESSION['phonecode'] == $compcode)
			{
				exit(json_encode(array('status'=>1,'url'=>'','msg'=>'验证码验证成功,赶紧注册吧..^_^')));
			}else{
				exit(json_encode(array('status'=>0,'url'=>'','msg'=>'验证码输入错误或者失效,赶紧注册吧..^_^')));
			}
		}
	}
	
	//对手机号和验证码验证成功后改密码
	public function passverifyphonecode()
	{
		if (IS_AJAX) 
		{
			$phone = $_POST['phone'];
			$code = $_POST['code'];
			$compcode = $phone.$code;
			if($_SESSION['phonecode'] == $compcode)
			{
				$UserModel = new UsersModel();
				if ($result = $UserModel->updatepass()) 
				{
					exit(json_encode(array('status'=>1,'url'=>'','msg'=>'设置新密码成功..^_^')));
				}
			}else{
				exit(json_encode(array('status'=>0,'url'=>'','msg'=>'验证码输入错误或者失效,赶紧注册吧..^_^')));
			}
		}
	}


	/**
	 * AJAX 手机登录操作
	 */
	public function do_wap_login(){

		if (IS_AJAX) {
			$UserModel = new UsersModel();
			$url = __ROOT__."/";
			if ($UserModel->do_wap_login()) {
				exit(json_encode(array('status'=>1,'url'=>$url,'msg'=>'登录成功,马上为您跳转..^_^')));
			} else {
				//验证失败
				exit(json_encode(array('status'=>0,'url'=>'','msg'=>$UserModel->getError())));
			}
		}
	}

	/**
	 * AJAX 注册操作
	 */
	public function do_wap_register(){

		if (IS_AJAX) {
			$UserModel = new UsersModel();
			$url = __ROOT__."/";

			if ($result = $UserModel->do_wap_Users()) {
				
				//验证成功 发送短信
				$_SESSION['user']=$result;
				
					//unset($_SESSION['user']);
					exit(json_encode(array('status'=>1,'url'=>$url,'msg'=>'注册成功.^_^')));
				
			} else {
				//验证失败
				exit(json_encode(array('status'=>0,'url'=>'','msg'=>$UserModel->getError())));
			}
		}

	}

	/**
	 * AJAX 注册操作
	 */
	public function do_register(){

		if (IS_AJAX) {
			$UserModel = new UsersModel();
			$url = __ROOT__."/";

			if ($result = $UserModel->do_Users()) {
				;
				//验证成功 发送短信
				$_SESSION['user']=$result;
				//发送密码到邮箱
				$error = $this->sendEmaill_to_User($result['password']);
				if ($error) {

					//邮箱发送失败 删除数据
					$UserModel->where(array('uid'=>$result['uid']))->delete();
					exit(json_encode(array('status'=>0,'url'=>'','msg'=>$error)));
				} else {
					//unset($_SESSION['user']);
					exit(json_encode(array('status'=>1,'url'=>$url,'msg'=>'注册成功,密码已发送到您的邮箱.^_^')));
				}
			} else {
				//验证失败
				exit(json_encode(array('status'=>0,'url'=>'','msg'=>$UserModel->getError())));
			}
		}

	}
	/**
	 * 忘记密码
	 */
		public function forget()
		{
				$this->display();
		}
		/**
		*  修改密码
		*/
		public function modifys()
		{
			   $this->display();
		}
		public function domodify()
		{
			   if (IS_AJAX) 
			   {
					$UserModel = new UsersModel();
					if ($result = $UserModel->alterpass()) 
					{
						exit(json_encode(array('status'=>1,'url'=>'','msg'=>'密码修改成功.^_^')));
					}else{
						exit(json_encode(array('status'=>0,'url'=>'','msg'=>'密码修改失败.^_^')));
					}
			   }
		}



	/**
	 * 调用验证码
	 */
	public function verifycode(){
		$config =    array(
			'fontSize'    =>    23,    // 验证码字体大小
			'length'      =>    4,     // 验证码位数
			'useNoise'    =>    false, // 关闭验证码杂点
		);
		$Verify =     new \Think\Verify($config);
		$Verify->entry();
	}

	/**
	 *  个人中心
	 */
	public function center()
	{
		$this->display();
	
	}


}